﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_Assignments : System.Web.UI.Page
{
    ILE_Global ileGlobal = new ILE_Global();

    AssignmentsBLL assignmentsLogic = new AssignmentsBLL();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            _tempCounter = 0;
            LoadQuizOngoing();
            LoadQuizUpcoming();
            LoadQuizEnded(DateTime.Now.AddDays(-3));
            LoadEssayOngoing();
            LoadEssayUpcoming();
            LoadEssayEnded(DateTime.Now.AddDays(-3));
        }
    }

    protected void lnkbtnRefresh_Click(object sender, EventArgs e)
    {
        UpdatePanel1.Update();
    }


    #region Quiz OnGoing

    private void LoadQuizOngoing()
    {
        Assignments.AssignmentsDataTable quizAssignments = assignmentsLogic.GetAssignmentsByType("Quiz");
        Assignments.AssignmentsDataTable ongoingQuizAssignments = new Assignments.AssignmentsDataTable();

        foreach (Assignments.AssignmentsRow quizAssignment in quizAssignments)
        {
            if (quizAssignment.StartDateTime < DateTime.Now && quizAssignment.EndDateTime > DateTime.Now)
            {
                ongoingQuizAssignments.ImportRow(quizAssignment);
            }
        }

        if (ongoingQuizAssignments.Count == 0)
        {
            RepeaterOngoingQuizzes.Visible = false;
            lblNoOngoingQuizzes.Visible = true;
        }
        else
        {
            RepeaterOngoingQuizzes.DataSource = ongoingQuizAssignments.Rows;
            RepeaterOngoingQuizzes.DataBind();
            RepeaterOngoingQuizzes.Visible = true;
            lblNoOngoingQuizzes.Visible = false;
        }
    }

    protected int _tempCounter
    {
        get
        {
            return (int)ViewState["_tempCounter"];
        }
        set
        {
            ViewState["_tempCounter"] = value;
        }
    }
    
    protected void RepeaterOngoingQuizzes_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            AssignmentsBLL assignmentsLogic = new AssignmentsBLL();
            Assignments.AssignmentsRow row = e.Item.DataItem as Assignments.AssignmentsRow;

            Label AssignmentTitle = e.Item.FindControl("lblAssignmentTitle") as Label;
            Label ClassTitle = e.Item.FindControl("lblClassTitle") as Label;
            Label RemainingTime = e.Item.FindControl("lblRemainingTime") as Label;
            Repeater Sections = e.Item.FindControl("RepeaterOngoingQuizzesSections") as Repeater;

            _tempCounter++;

            double _remainingMinutes = row.EndDateTime.Subtract(DateTime.Now).TotalMinutes;
            if (_remainingMinutes > (60 * 24))
            {
                int Days = (Convert.ToInt32(_remainingMinutes / 60 / 24));
                RemainingTime.Text = Days.ToString() + " day(s)";
            }
            else if (_remainingMinutes < (60 * 24) &&
                     _remainingMinutes > 60)
            {
                int Hours = (Convert.ToInt32(_remainingMinutes / 60));
                int Minutes = ((Convert.ToInt32(_remainingMinutes)) % 60);
                RemainingTime.Text = Hours.ToString() + " hour(s) " + Minutes.ToString() + " minute(s)";
            }
            else if (_remainingMinutes < 60)
            {
                int Minutes = ((Convert.ToInt32(_remainingMinutes)) % 60);
                RemainingTime.Text = Minutes.ToString() + " minute(s)";
            }

            AssignmentTitle.Text = "<b>\"" + row.AssignmentTitle + "\"</b>";
            AssignmentTitle.ToolTip = "AssignmentID: " + row.AssignmentID.ToString();

            Assignments.RelationsDataTable relations = assignmentsLogic.GetRelationsByAssignmentID(row.AssignmentID);

            try
            {
                int CourseID = relations[0].CourseID;
                ClassBLL classLogic = new ClassBLL();
                ClassTitle.Text = "<b>" + classLogic.GetCourse(CourseID).Title + "</b>";
            }
            catch (IndexOutOfRangeException ex)
            {
                ClassTitle.Text = "<span style='color: Red; font-weight: bold;'>Error with this assignment! Recommend you to delete it.</span>";
                ileGlobal.RecordError("Assignments.aspx.cs:RepeaterOngoingQuizzes_ItemDataBound:000", ex, 1, Request.Browser);
            }

            Sections.DataSource = relations.Rows;
            Sections.DataBind();
        }
    }

    #endregion


    #region Quiz Upcoming

    private void LoadQuizUpcoming()
    {
        Assignments.AssignmentsDataTable quizAssignments = assignmentsLogic.GetAssignmentsByType("Quiz");
        Assignments.AssignmentsDataTable pedingQuizAssignments = new Assignments.AssignmentsDataTable();

        foreach (Assignments.AssignmentsRow quizAssignment in quizAssignments)
        {
            if (quizAssignment.StartDateTime > DateTime.Now)
            {
                pedingQuizAssignments.ImportRow(quizAssignment);
            }
        }

        if (pedingQuizAssignments.Count == 0)
        {
            RepeaterUpcomingQuizzes.Visible = false;
            lblNoUpcomingQuizzes.Visible = true;
        }
        else
        {
            RepeaterUpcomingQuizzes.DataSource = pedingQuizAssignments.Rows;
            RepeaterUpcomingQuizzes.DataBind();
            RepeaterUpcomingQuizzes.Visible = true;
            lblNoUpcomingQuizzes.Visible = false;
        }
    }

    protected void RepeaterUpcomingQuizzes_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            AssignmentsBLL assignmentsLogic = new AssignmentsBLL();
            Assignments.AssignmentsRow row = e.Item.DataItem as Assignments.AssignmentsRow;

            Label RemainingTime = e.Item.FindControl("lblRemainingTime") as Label;
            Label AssignmentTitle = e.Item.FindControl("lblAssignmentTitle") as Label;
            Label ClassTitle = e.Item.FindControl("lblClassTitle") as Label;
            Repeater Sections = e.Item.FindControl("RepeaterUpcomingQuizzesSections") as Repeater;

            _tempCounter++;

            double _remainingMinutes = row.StartDateTime.Subtract(DateTime.Now).TotalMinutes;
            if (_remainingMinutes > (60 * 24))
            {
                int Days = (Convert.ToInt32(_remainingMinutes / 60 / 24));
                RemainingTime.Text = Days.ToString() + " day(s)";
            }
            else if (_remainingMinutes  < (60 * 24) &&
                     _remainingMinutes > 60)
            {
                int Hours = (Convert.ToInt32(_remainingMinutes / 60));
                int Minutes = ((Convert.ToInt32(_remainingMinutes)) % 60);
                RemainingTime.Text = Hours.ToString() + " hour(s) " + Minutes.ToString() + " minute(s)";
            }
            else if (_remainingMinutes < 60)
            {
                int Minutes = ((Convert.ToInt32(_remainingMinutes)) % 60);
                RemainingTime.Text = Minutes.ToString() + " minute(s)";
            }

            AssignmentTitle.Text = "<b>\"" + row.AssignmentTitle + "\"</b>";
            AssignmentTitle.ToolTip = "AssignmentID: " + row.AssignmentID.ToString();

            Assignments.RelationsDataTable relations = assignmentsLogic.GetRelationsByAssignmentID(row.AssignmentID);

            try
            {
                int CourseID = relations[0].CourseID;
                ClassBLL classLogic = new ClassBLL();
                ClassTitle.Text = "<b>" + classLogic.GetCourse(CourseID).Title + "</b>";
            }
            catch (IndexOutOfRangeException ex)
            {
                ClassTitle.Text = "<span style='color: Red; font-weight: bold;'>Error with this assignment! Recommend you to delete it.</span>";
                ileGlobal.RecordError("Assignments.aspx.cs:RepeaterUpcomingQuizzes_ItemDataBound:000", ex, 1, Request.Browser);
            }

            Sections.DataSource = relations.Rows;
            Sections.DataBind();
        }
    }

    #endregion


    #region Quiz Ended

    protected void LoadQuizEnded(DateTime endCutTime)
    {
        Assignments.AssignmentsDataTable quizAssignments = assignmentsLogic.GetAssignmentsByType("Quiz");
        Assignments.AssignmentsDataTable endedQuizAssignments = new Assignments.AssignmentsDataTable();

        foreach (Assignments.AssignmentsRow quizAssignment in quizAssignments)
        {
            if (quizAssignment.EndDateTime > endCutTime && quizAssignment.EndDateTime < DateTime.Now)
            {
                endedQuizAssignments.ImportRow(quizAssignment);
            }
        }

        if (endedQuizAssignments.Count == 0)
        {
            RepeaterEndedQuizzes.Visible = false;
            string temp = String.Empty;
            switch (ddlistQuizEndedDayOption.SelectedValue)
            {
                case "All":
                    temp = "";
                    break;
                default:
                    temp = " for the last " + ddlistQuizEndedDayOption.SelectedValue + " days";
                    break;
            }
            lblNoEndedQuizzes.Text = "<div style='width: 550px; text-align: center; font-weight: bold; font-size: 9pt; padding-bottom: 10px; color: #999;'>" + 
                "There is no ended quiz" + temp + ".</div>";
            lblNoEndedQuizzes.Visible = true;
        }
        else
        {
            RepeaterEndedQuizzes.DataSource = endedQuizAssignments.Rows;
            RepeaterEndedQuizzes.DataBind();
            RepeaterEndedQuizzes.Visible = true;
            lblNoEndedQuizzes.Visible = false;
        }
    }

    protected void RepeaterEndedQuizzes_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            AssignmentsBLL assignmentsLogic = new AssignmentsBLL();
            Assignments.AssignmentsRow row = e.Item.DataItem as Assignments.AssignmentsRow;

            Label PassedTime = e.Item.FindControl("lblPassedTime") as Label;
            Label AssignmentTitle = e.Item.FindControl("lblAssignmentTitle") as Label;
            Label ClassTitle = e.Item.FindControl("lblClassTitle") as Label;
            Repeater Sections = e.Item.FindControl("RepeaterEndedQuizzesSections") as Repeater;

            _tempCounter++;

            double _passedMinutes = DateTime.Now.Subtract(row.EndDateTime).TotalMinutes;
            if (_passedMinutes > (60 * 24))
            {
                int Days = (Convert.ToInt32(_passedMinutes / 60 / 24));
                PassedTime.Text = Days.ToString() + " day(s)";
            }
            else if (_passedMinutes < (60 * 24) &&
                     _passedMinutes > 60)
            {
                int Hours = (Convert.ToInt32(_passedMinutes / 60));
                int Minutes = ((Convert.ToInt32(_passedMinutes)) % 60);
                PassedTime.Text = Hours.ToString() + " hour(s) " + Minutes.ToString() + " minute(s)";
            }
            else if (_passedMinutes < 60)
            {
                int Minutes = ((Convert.ToInt32(_passedMinutes)) % 60);
                PassedTime.Text = Minutes.ToString() + " minute(s)";
            }

            AssignmentTitle.Text = "<b>\"" + row.AssignmentTitle + "\"</b>";
            AssignmentTitle.ToolTip = "AssignmentID: " + row.AssignmentID.ToString();

            Assignments.RelationsDataTable relations = assignmentsLogic.GetRelationsByAssignmentID(row.AssignmentID);

            try
            {
                int CourseID = relations[0].CourseID;
                ClassBLL classLogic = new ClassBLL();
                ClassTitle.Text = "<b>" + classLogic.GetCourse(CourseID).Title + "</b>";
            }
            catch (IndexOutOfRangeException ex)
            {
                ClassTitle.Text = "<span style='color: Red; font-weight: bold;'>Error with this assignment! Recommend you to delete it.</span>";
                ileGlobal.RecordError("Assignments.aspx.cs:RepeaterEndedQuizzes_ItemDataBound:000", ex, 1, Request.Browser);
            }

            Sections.DataSource = relations.Rows;
            Sections.DataBind();
        }
    }

    protected void ddlistQuizEndedDayOption_SelectedIndexChanged(object sender, EventArgs e)
    {
        DateTime endCutTime = new DateTime();
        switch (((DropDownList)sender).SelectedValue)
        {
            case "3":
                endCutTime = DateTime.Now.AddDays(-3);
                break;
            case "7":
                endCutTime = DateTime.Now.AddDays(-7);
                break;
            case "14":
                endCutTime = DateTime.Now.AddDays(-14);
                break;
            case "30":
                endCutTime = DateTime.Now.AddDays(-30);
                break;
            case "All":
                endCutTime = DateTime.Now.AddYears(-100);
                break;
            default:
                ((DropDownList)sender).SelectedIndex = 0;
                endCutTime = endCutTime = DateTime.Now.AddDays(-3);
                break;
        }

        LoadQuizEnded(endCutTime);
    }

    #endregion


    #region Quiz Shared

    protected void RepeaterQuizzesSections_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            AssignmentsBLL assignmentsLogic = new AssignmentsBLL();
            Assignments.RelationsRow row = e.Item.DataItem as Assignments.RelationsRow;

            Label Sections = e.Item.FindControl("lblSectionNumber") as Label;

            Sections.Text = row.SectionNumber.ToString();
        }
    }
    
    #endregion

    #region Essay OnGoing

    private void LoadEssayOngoing()
    {
        Assignments.AssignmentsDataTable essayAssignments = assignmentsLogic.GetAssignmentsByType("Essay");
        Assignments.AssignmentsDataTable ongoingEssayAssignments = new Assignments.AssignmentsDataTable();

        foreach (Assignments.AssignmentsRow essayAssignment in essayAssignments)
        {
            if (essayAssignment.StartDateTime < DateTime.Now && essayAssignment.EndDateTime > DateTime.Now)
            {
                ongoingEssayAssignments.ImportRow(essayAssignment);
            }
        }

        if (ongoingEssayAssignments.Count == 0)
        {
            RepeaterOngoingEssays.Visible = false;
            lblNoOngoingEssays.Visible = true;
        }
        else
        {
            RepeaterOngoingEssays.DataSource = ongoingEssayAssignments.Rows;
            RepeaterOngoingEssays.DataBind();
            RepeaterOngoingEssays.Visible = true;
            lblNoOngoingEssays.Visible = false;
        }
    }

    protected void RepeaterOngoingEssays_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            AssignmentsBLL assignmentsLogic = new AssignmentsBLL();
            Assignments.AssignmentsRow row = e.Item.DataItem as Assignments.AssignmentsRow;

            Label AssignmentTitle = e.Item.FindControl("lblAssignmentTitle") as Label;
            Label ClassTitle = e.Item.FindControl("lblClassTitle") as Label;
            Label RemainingTime = e.Item.FindControl("lblRemainingTime") as Label;
            Repeater Sections = e.Item.FindControl("RepeaterOngoingEssaysSections") as Repeater;

            _tempCounter++;

            double _remainingMinutes = row.EndDateTime.Subtract(DateTime.Now).TotalMinutes;
            if (_remainingMinutes > (60 * 24))
            {
                int Days = (Convert.ToInt32(_remainingMinutes / 60 / 24));
                RemainingTime.Text = Days.ToString() + " day(s)";
            }
            else if (_remainingMinutes < (60 * 24) &&
                     _remainingMinutes > 60)
            {
                int Hours = (Convert.ToInt32(_remainingMinutes / 60));
                int Minutes = ((Convert.ToInt32(_remainingMinutes)) % 60);
                RemainingTime.Text = Hours.ToString() + " hour(s) " + Minutes.ToString() + " minute(s)";
            }
            else if (_remainingMinutes < 60)
            {
                int Minutes = ((Convert.ToInt32(_remainingMinutes)) % 60);
                RemainingTime.Text = Minutes.ToString() + " minute(s)";
            }

            AssignmentTitle.Text = "<b>\"" + row.AssignmentTitle + "\"</b>";
            AssignmentTitle.ToolTip = "AssignmentID: " + row.AssignmentID.ToString();

            Assignments.RelationsDataTable relations = assignmentsLogic.GetRelationsByAssignmentID(row.AssignmentID);

            try
            {
                int CourseID = relations[0].CourseID;
                ClassBLL classLogic = new ClassBLL();
                ClassTitle.Text = "<b>" + classLogic.GetCourse(CourseID).Title + "</b>";
            }
            catch (IndexOutOfRangeException ex)
            {
                ClassTitle.Text = "<span style='color: Red; font-weight: bold;'>Error with this assignment! Recommend you to delete it.</span>";

                ileGlobal.RecordError("Assignments.aspx.cs:RepeaterOngoingEssays_ItemDataBound:000", ex, 1, Request.Browser);
            }

            Sections.DataSource = relations.Rows;
            Sections.DataBind();
        }
    }

    #endregion


    #region Essay Upcoming

    private void LoadEssayUpcoming()
    {
        Assignments.AssignmentsDataTable essayAssignments = assignmentsLogic.GetAssignmentsByType("Essay");
        Assignments.AssignmentsDataTable pedingEssayAssignments = new Assignments.AssignmentsDataTable();

        foreach (Assignments.AssignmentsRow essayAssignment in essayAssignments)
        {
            if (essayAssignment.StartDateTime > DateTime.Now)
            {
                pedingEssayAssignments.ImportRow(essayAssignment);
            }
        }

        if (pedingEssayAssignments.Count == 0)
        {
            RepeaterUpcomingEssays.Visible = false;
            lblNoUpcomingEssays.Visible = true;
        }
        else
        {
            RepeaterUpcomingEssays.DataSource = pedingEssayAssignments.Rows;
            RepeaterUpcomingEssays.DataBind();
            RepeaterUpcomingEssays.Visible = true;
            lblNoUpcomingEssays.Visible = false;
        }
    }

    protected void RepeaterUpcomingEssays_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            AssignmentsBLL assignmentsLogic = new AssignmentsBLL();
            Assignments.AssignmentsRow row = e.Item.DataItem as Assignments.AssignmentsRow;

            Label RemainingTime = e.Item.FindControl("lblRemainingTime") as Label;
            Label AssignmentTitle = e.Item.FindControl("lblAssignmentTitle") as Label;
            Label ClassTitle = e.Item.FindControl("lblClassTitle") as Label;
            Repeater Sections = e.Item.FindControl("RepeaterUpcomingEssaysSections") as Repeater;

            _tempCounter++;

            double _remainingMinutes = row.StartDateTime.Subtract(DateTime.Now).TotalMinutes;
            if (_remainingMinutes > (60 * 24))
            {
                int Days = (Convert.ToInt32(_remainingMinutes / 60 / 24));
                RemainingTime.Text = Days.ToString() + " day(s)";
            }
            else if (_remainingMinutes < (60 * 24) &&
                     _remainingMinutes > 60)
            {
                int Hours = (Convert.ToInt32(_remainingMinutes / 60));
                int Minutes = ((Convert.ToInt32(_remainingMinutes)) % 60);
                RemainingTime.Text = Hours.ToString() + " hour(s) " + Minutes.ToString() + " minute(s)";
            }
            else if (_remainingMinutes < 60)
            {
                int Minutes = ((Convert.ToInt32(_remainingMinutes)) % 60);
                RemainingTime.Text = Minutes.ToString() + " minute(s)";
            }

            AssignmentTitle.Text = "<b>\"" + row.AssignmentTitle + "\"</b>";
            AssignmentTitle.ToolTip = "AssignmentID: " + row.AssignmentID.ToString();

            Assignments.RelationsDataTable relations = assignmentsLogic.GetRelationsByAssignmentID(row.AssignmentID);

            try
            {
                int CourseID = relations[0].CourseID;
                ClassBLL classLogic = new ClassBLL();
                ClassTitle.Text = "<b>" + classLogic.GetCourse(CourseID).Title + "</b>";
            }
            catch (IndexOutOfRangeException ex)
            {
                ClassTitle.Text = "<span style='color: Red; font-weight: bold;'>Error with this assignment! Recommend you to delete it.</span>";
                ileGlobal.RecordError("Assignments.aspx.cs:RepeaterUpcomingEssays_ItemDataBound:000", ex, 1, Request.Browser);
            }

            Sections.DataSource = relations.Rows;
            Sections.DataBind();
        }
    }

    #endregion


    #region Essay Ended

    protected void LoadEssayEnded(DateTime endCutTime)
    {
        Assignments.AssignmentsDataTable essayAssignments = assignmentsLogic.GetAssignmentsByType("Essay");
        Assignments.AssignmentsDataTable endedEssayAssignments = new Assignments.AssignmentsDataTable();

        foreach (Assignments.AssignmentsRow essayAssignment in essayAssignments)
        {
            if (essayAssignment.EndDateTime > endCutTime && essayAssignment.EndDateTime < DateTime.Now)
            {
                endedEssayAssignments.ImportRow(essayAssignment);
            }
        }

        if (endedEssayAssignments.Count == 0)
        {
            RepeaterEndedEssays.Visible = false;
            string temp = String.Empty;
            switch (ddlistEssayEndedDayOption.SelectedValue)
            {
                case "All":
                    temp = "";
                    break;
                default:
                    temp = " for the last " + ddlistEssayEndedDayOption.SelectedValue + " days";
                    break;
            }
            lblNoEndedEssays.Text = "<div style='width: 550px; text-align: center; font-weight: bold; font-size: 9pt; padding-bottom: 10px; color: #999;'>" +
                "There is no ended essay" + temp + ".</div>";
            lblNoEndedEssays.Visible = true;
        }
        else
        {
            RepeaterEndedEssays.DataSource = endedEssayAssignments.Rows;
            RepeaterEndedEssays.DataBind();
            RepeaterEndedEssays.Visible = true;
            lblNoEndedEssays.Visible = false;
        }
    }

    protected void RepeaterEndedEssays_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            AssignmentsBLL assignmentsLogic = new AssignmentsBLL();
            Assignments.AssignmentsRow row = e.Item.DataItem as Assignments.AssignmentsRow;

            Label PassedTime = e.Item.FindControl("lblPassedTime") as Label;
            Label AssignmentTitle = e.Item.FindControl("lblAssignmentTitle") as Label;
            Label ClassTitle = e.Item.FindControl("lblClassTitle") as Label;
            Repeater Sections = e.Item.FindControl("RepeaterEndedEssaysSections") as Repeater;

            _tempCounter++;

            double _passedMinutes = DateTime.Now.Subtract(row.EndDateTime).TotalMinutes;
            if (_passedMinutes > (60 * 24))
            {
                int Days = (Convert.ToInt32(_passedMinutes / 60 / 24));
                PassedTime.Text = Days.ToString() + " day(s)";
            }
            else if (_passedMinutes < (60 * 24) &&
                     _passedMinutes > 60)
            {
                int Hours = (Convert.ToInt32(_passedMinutes / 60));
                int Minutes = ((Convert.ToInt32(_passedMinutes)) % 60);
                PassedTime.Text = Hours.ToString() + " hour(s) " + Minutes.ToString() + " minute(s)";
            }
            else if (_passedMinutes < 60)
            {
                int Minutes = ((Convert.ToInt32(_passedMinutes)) % 60);
                PassedTime.Text = Minutes.ToString() + " minute(s)";
            }

            AssignmentTitle.Text = "<b>\"" + row.AssignmentTitle + "\"</b>";
            AssignmentTitle.ToolTip = "AssignmentID: " + row.AssignmentID.ToString();

            Assignments.RelationsDataTable relations = assignmentsLogic.GetRelationsByAssignmentID(row.AssignmentID);

            try
            {
                int CourseID = relations[0].CourseID;
                ClassBLL classLogic = new ClassBLL();
                ClassTitle.Text = "<b>" + classLogic.GetCourse(CourseID).Title + "</b>";
            }
            catch (IndexOutOfRangeException ex)
            {
                ClassTitle.Text = "<span style='color: Red; font-weight: bold;'>Error with this assignment! Recommend you to delete it.</span>";
                ileGlobal.RecordError("Assignments.aspx.cs:RepeaterEndedEssays_ItemDataBound:000", ex, 1, Request.Browser);
            }

            Sections.DataSource = relations.Rows;
            Sections.DataBind();
        }
    }

    protected void ddlistEssayEndedDayOption_SelectedIndexChanged(object sender, EventArgs e)
    {
        DateTime endCutTime = new DateTime();
        switch (((DropDownList)sender).SelectedValue)
        {
            case "3":
                endCutTime = DateTime.Now.AddDays(-3);
                break;
            case "7":
                endCutTime = DateTime.Now.AddDays(-7);
                break;
            case "14":
                endCutTime = DateTime.Now.AddDays(-14);
                break;
            case "30":
                endCutTime = DateTime.Now.AddDays(-30);
                break;
            case "All":
                endCutTime = DateTime.Now.AddYears(-100);
                break;
            default:
                ((DropDownList)sender).SelectedIndex = 0;
                endCutTime = endCutTime = DateTime.Now.AddDays(-3);
                break;
        }

        LoadEssayEnded(endCutTime);
    }

    #endregion


    #region Essay Shared

    protected void RepeaterEssaysSections_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            AssignmentsBLL assignmentsLogic = new AssignmentsBLL();
            Assignments.RelationsRow row = e.Item.DataItem as Assignments.RelationsRow;

            Label Sections = e.Item.FindControl("lblSectionNumber") as Label;

            Sections.Text = row.SectionNumber.ToString();
        }
    }

    #endregion


}
